package com.ruoyi.web.controller.storage;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.core.page.TableDataInfo;
import com.ruoyi.common.core.text.Convert;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.purchase.domain.Brand;
import com.ruoyi.storage.domain.RepoWare;
import com.ruoyi.storage.service.RepoWareService;
import com.ruoyi.system.domain.SysCity;
import com.ruoyi.system.domain.SysProvince;
import com.ruoyi.system.service.ISysUserService;
import com.ruoyi.system.service.SysCityService;
import com.ruoyi.system.service.SysProvinceService;
import org.apache.commons.lang3.ArrayUtils;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.*;

import java.util.Arrays;
import java.util.Date;
import java.util.List;

@Controller
@RequestMapping("/storage/ware")
public class WareManageController extends BaseController {

    private String prefix = "/storage/ware";


    @Autowired
    private RepoWareService repoWareService;
    @Autowired
    private SysProvinceService sysProvinceService;
    @Autowired
    private SysCityService sysCityService;
    @Autowired
    private ISysUserService iSysUserService;


    @RequiresPermissions("storage:ware:view")
    @GetMapping()
    public String ware( Model model)
    {
        List<SysProvince> provinceList = sysProvinceService.list();
        List<SysCity> cityList = sysCityService.list();
        model.addAttribute("provinceList",provinceList);
        model.addAttribute("cityList",cityList);
        return prefix + "/ware";
    }


    /**
     * 查看仓库列表
     * @param queryWareName 根据仓库名进行查询
     * @param queryWareCity 根据所属地区来查找
     * @return
     */
    @RequiresPermissions("storage:ware:list")
    @RequestMapping("/list")
    @ResponseBody
    public TableDataInfo list(String queryWareName,Integer queryWareCity)
    {
        startPage();
//        QueryWrapper<RepoWare> queryWrapper = new QueryWrapper<>();
//        if (queryWareName!=null && queryWareName!=""){
//            queryWrapper.like("ware_name",queryWareName);
//        }
//        if (queryWareCity!=null && queryWareCity!=0){
//            queryWrapper.eq("sys_city_id",queryWareCity);
//        }
        List<RepoWare> list = repoWareService.selectAllWare(queryWareName,queryWareCity);
        return getDataTable(list);
    }

    /**
     * 创建仓库是实现跳转
     */
    @GetMapping("/add")
    public String add(Model model)
    {
        List<SysProvince> provinceList = sysProvinceService.list();
        List<SysCity> cityList = sysCityService.list();
        SysUser sysUser = new SysUser();
        List<SysUser> sysUsers = iSysUserService.selectUserList(sysUser);
        model.addAttribute("provinceList",provinceList);
        model.addAttribute("cityList",cityList);
        model.addAttribute("sysUsers",sysUsers);
        return prefix + "/add";
    }


    /**
     * 新增保存仓库信息
     */
    @RequiresPermissions("storage:ware:add")
    @Log(title = "仓库信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RepoWare repoWare)
    {
        repoWare.setCreateTime(new Date());
//        repoWare.setSysCreateUserId(1);
        repoWare.setSysCreateUserId(getUserId().intValue());
        return toAjax(repoWareService.save(repoWare));
    }

    /**
     * 修改信息
     */
    @GetMapping("/edit/{bid}")
    public String edit(@PathVariable("bid") Integer wareId, ModelMap mmap)
    {
        RepoWare repoWare = repoWareService.getById(wareId);
        List<SysProvince> provinceList = sysProvinceService.list();
        List<SysCity> cityList = sysCityService.list();
        mmap.addAttribute("provinceList",provinceList);
        mmap.addAttribute("cityList",cityList);
        mmap.put("repoWare", repoWare);
        return prefix + "/edit";
    }

    /**
     * 修改保存信息
     */
    @RequiresPermissions("storage:ware:edit")
    @Log(title = "仓库信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RepoWare repoWare)
    {
        return toAjax(repoWareService.updateById(repoWare));
    }

    /**
     * 导出仓库信息列表
     */
    @RequiresPermissions("storage:ware:export")
    @Log(title = "仓库信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(String queryWareName,Integer queryWareCity)
    {
        QueryWrapper<RepoWare> queryWrapper = new QueryWrapper<>();
        if (queryWareName!=null && queryWareName!=""){
            queryWrapper.like("ware_name",queryWareName);
        }
        if (queryWareCity!=null && queryWareCity!=0){
            queryWrapper.eq("sys_city_id",queryWareCity);
        }
        List<RepoWare> list = repoWareService.list(queryWrapper);
        ExcelUtil<RepoWare> util = new ExcelUtil<RepoWare>(RepoWare.class);
        return util.exportExcel(list, "repoWare");
    }

    /**
     * 删除仓库信息
     */
    @RequiresPermissions("storage:ware:remove")
    @Log(title = "仓库信息", businessType = BusinessType.DELETE)
    @PostMapping("/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        if (ArrayUtils.contains(Convert.toLongArray(ids), getUserId()))
        {
            return error("当前用户不能删除");
        }
        return toAjax(repoWareService.deleteUserByIds(ids));
    }
}
